/*
 * application_main.c
 *
 *  Created on: Aug 9, 2013
 *      Author: CongThuan
 */
#include"../h/app_types.h"
#define TEMP_CLIENT_ADDR "192.168.1.129"
typedef struct{
	U8 number_message;
	U32 msg_id;
	U16 msg_len;
	U32 src_id;
	U32 dst_id;
	struct sockaddr_in src_addr;
	struct sockaddr_in dst_addr;
}infomation_message_test_t;

infomation_message_test_t g_test_message[30];


/***********************************************************************
 * FUNCTION NAME:
 *  app_init_func
 *
 * DESCRIPTION:
 * initial all global variable and other initialization.
 * initial for output_message_queue of application_layer
 *
 * INPUT:
 *
 * OUTPUT:
 *
 * RETURN:
 *	val_result
 * GLOBAL DATA REFERRED:
 *
 * CALLED ROUTINES:
 *
 * NOTES:
 *
 ************************************************************************/
U8 app_init_func()
{
	U8 val_result = SYSLOG_FALSE;

	/*Create TRANSPORT_INPUT_MQUEUE*/
	val_result = app_create_output_queues_func();
	if(SYSLOG_FAILURE == val_result)
	{
		SYSLOG_TRANSPORT_ERROR_LOGGING("APPLICATION_OUTPUT_MQUEUE: msgget failure");
		SYSLOG_TRANSPORT_ERROR_LOGGING("APPLICATION_OUTPUT_MQUEUE: Can't create message_queue for APPLICATION_OUTPUT_MQUEUE");
		return SYSLOG_TRUE;
	}
	return val_result;
}
void app_init_list_message_func()
{
	/*MSG1*/
	g_test_message[0].number_message = 7;
	g_test_message[0].msg_id = 1;
	g_test_message[0].msg_len = sizeof(transport_msg_union_t);
	g_test_message[0].src_id = TASK_ID_APPLICATION;
	g_test_message[0].dst_id = TRANSPORT_INPUT_MQUEUE;
	g_test_message[0].src_addr.sin_addr.s_addr = inet_addr(TEMP_CLIENT_ADDR);
	g_test_message[0].src_addr.sin_port = htons(1019);
	g_test_message[0].dst_addr.sin_addr.s_addr = inet_addr(TEMP_CLIENT_ADDR);
	g_test_message[0].dst_addr.sin_port = htons(1020);

	/*MSG2*/
	g_test_message[1].number_message = 13;
	g_test_message[1].msg_id = 3;
	g_test_message[1].msg_len = sizeof(transport_msg_union_t);
	g_test_message[1].src_id = TASK_ID_APPLICATION;
	g_test_message[1].dst_id = TRANSPORT_INPUT_MQUEUE;
	g_test_message[1].src_addr.sin_addr.s_addr = inet_addr(TEMP_CLIENT_ADDR);
	g_test_message[1].src_addr.sin_port = htons(1019);
	g_test_message[1].dst_addr.sin_addr.s_addr = inet_addr(TEMP_CLIENT_ADDR);
	g_test_message[1].dst_addr.sin_port = htons(1021);

	/*MSG3*/
	g_test_message[2].number_message = 16;
	g_test_message[2].msg_id = 2;
	g_test_message[2].msg_len = sizeof(transport_msg_union_t);
	g_test_message[2].src_id = TASK_ID_APPLICATION;
	g_test_message[2].dst_id = TRANSPORT_INPUT_MQUEUE;
	g_test_message[2].src_addr.sin_addr.s_addr = inet_addr(TEMP_CLIENT_ADDR);
	g_test_message[2].src_addr.sin_port = htons(1019);
	g_test_message[2].dst_addr.sin_addr.s_addr = inet_addr(TEMP_CLIENT_ADDR);
	g_test_message[2].dst_addr.sin_port = htons(1022);

	/*MSG4*/
	g_test_message[3].number_message = 4;
	g_test_message[3].msg_id = 1;
	g_test_message[3].msg_len = sizeof(transport_msg_union_t);
	g_test_message[3].src_id = TASK_ID_APPLICATION;
	g_test_message[3].dst_id = TRANSPORT_INPUT_MQUEUE;
	g_test_message[3].src_addr.sin_addr.s_addr = inet_addr(TEMP_CLIENT_ADDR);
	g_test_message[3].src_addr.sin_port = htons(1019);
	g_test_message[3].dst_addr.sin_addr.s_addr = inet_addr(TEMP_CLIENT_ADDR);
	g_test_message[3].dst_addr.sin_port = htons(1023);

	/*MSG4*/
	g_test_message[4].number_message = 5;
	g_test_message[4].msg_id = 1;
	g_test_message[4].msg_len = sizeof(transport_msg_union_t);
	g_test_message[4].src_id = TASK_ID_APPLICATION;
	g_test_message[4].dst_id = TRANSPORT_INPUT_MQUEUE;
	g_test_message[4].src_addr.sin_addr.s_addr = inet_addr(TEMP_CLIENT_ADDR);
	g_test_message[4].src_addr.sin_port = htons(1019);
	g_test_message[4].dst_addr.sin_addr.s_addr = inet_addr(TEMP_CLIENT_ADDR);
	g_test_message[4].dst_addr.sin_port = htons(1024);

	/*MS5*/
	g_test_message[5].number_message = 15;
	g_test_message[5].msg_id = 1;
	g_test_message[5].msg_len = sizeof(transport_msg_union_t);
	g_test_message[5].src_id = TASK_ID_APPLICATION;
	g_test_message[5].dst_id = TRANSPORT_INPUT_MQUEUE;
	g_test_message[5].src_addr.sin_addr.s_addr = inet_addr(TEMP_CLIENT_ADDR);
	g_test_message[5].src_addr.sin_port = htons(1019);
	g_test_message[5].dst_addr.sin_addr.s_addr = inet_addr(TEMP_CLIENT_ADDR);
	g_test_message[5].dst_addr.sin_port = htons(1025);

	/*MSG6*/
	g_test_message[6].number_message = 7;
	g_test_message[6].msg_id = 1;
	g_test_message[6].msg_len = sizeof(transport_msg_union_t);
	g_test_message[6].src_id = TASK_ID_APPLICATION;
	g_test_message[6].dst_id = TRANSPORT_INPUT_MQUEUE;
	g_test_message[6].src_addr.sin_addr.s_addr = inet_addr(TEMP_CLIENT_ADDR);
	g_test_message[6].src_addr.sin_port = htons(1019);
	g_test_message[6].dst_addr.sin_addr.s_addr = inet_addr(TEMP_CLIENT_ADDR);
	g_test_message[6].dst_addr.sin_port = htons(1026);

	/*MSG7*/
	g_test_message[7].number_message = 3;
	g_test_message[7].msg_id = 3;
	g_test_message[7].msg_len = sizeof(transport_msg_union_t);
	g_test_message[7].src_id = TASK_ID_APPLICATION;
	g_test_message[7].dst_id = TRANSPORT_INPUT_MQUEUE;
	g_test_message[7].src_addr.sin_addr.s_addr = inet_addr(TEMP_CLIENT_ADDR);
	g_test_message[7].src_addr.sin_port = htons(1019);
	g_test_message[7].dst_addr.sin_addr.s_addr = inet_addr(TEMP_CLIENT_ADDR);
	g_test_message[7].dst_addr.sin_port = htons(1027);

	/*MSG7*/
	g_test_message[8].number_message = 6;
	g_test_message[8].msg_id = 3;
	g_test_message[8].msg_len = sizeof(transport_msg_union_t);
	g_test_message[8].src_id = TASK_ID_APPLICATION;
	g_test_message[8].dst_id = TRANSPORT_INPUT_MQUEUE;
	g_test_message[8].src_addr.sin_addr.s_addr = inet_addr(TEMP_CLIENT_ADDR);
	g_test_message[8].src_addr.sin_port = htons(1019);
	g_test_message[8].dst_addr.sin_addr.s_addr = inet_addr(TEMP_CLIENT_ADDR);
	g_test_message[8].dst_addr.sin_port = htons(1028);

	/*MSG7*/
	g_test_message[9].number_message = 4;
	g_test_message[9].msg_id = 3;
	g_test_message[9].msg_len = sizeof(transport_msg_union_t);
	g_test_message[9].src_id = TASK_ID_APPLICATION;
	g_test_message[9].dst_id = TRANSPORT_INPUT_MQUEUE;
	g_test_message[9].src_addr.sin_addr.s_addr = inet_addr(TEMP_CLIENT_ADDR);
	g_test_message[9].src_addr.sin_port = htons(1019);
	g_test_message[9].dst_addr.sin_addr.s_addr = inet_addr(TEMP_CLIENT_ADDR);
	g_test_message[9].dst_addr.sin_port = htons(1029);

	//80 message will send to INPUT_QUEUE of transport
}
/***********************************************************************
 * FUNCTION NAME:
 *  app_init_func
 *
 * DESCRIPTION:
 * initial all global variable and other initialization.
 * initial for output_message_queue of application_layer
 *
 * INPUT:
 *
 * OUTPUT:
 *
 * RETURN:
 *	val_result
 * GLOBAL DATA REFERRED:
 *
 * CALLED ROUTINES:
 *
 * NOTES:
 *
 ************************************************************************/
void app_send_message_to_transport_input_queue_func()
{
	transport_msg_union_t *msg;
	U8 i = 0;
	SYSLOG_APPLICATION_INFO_LOGGING("Enter function app_send_message_to_transport_input_queue_func");
	msg = T_OS_BLOCK_ALLOC(TASK_ID_APPLICATION,sizeof(transport_msg_union_t));


	for(i = 0;i<10;i++)
	{

		if(g_test_message[i].number_message > 0)
		{
			/*send message*/

			msg->msg_hdr.msg_id = g_test_message[i].msg_id;
			msg->msg_hdr.dst_id = g_test_message[i].dst_id;
			msg->msg_hdr.src_id = g_test_message[i].src_id;
			msg->msg_hdr.msg_len= g_test_message[i].msg_len;
			msg->msg_hdr.src_addr= g_test_message[i].src_addr;
			msg->msg_hdr.dst_addr= g_test_message[i].dst_addr;

			strcpy(msg->msg_body.exchange_param.content,"00000000000");


			T_OS_SEND_MSG_QUEUE(g_test_message[i].src_id,g_test_message[i].dst_id,\
					msg,g_test_message[i].msg_len);


			g_test_message[i].number_message--;
			SYSLOG_APPLICATION_INFO_LOGGING("send message[%d] number_message[%d]",i,g_test_message[i].number_message);
			memset(msg,0,sizeof(transport_msg_union_t));
		}
	}
	T_OS_BLOCK_DEALLOC(TASK_ID_APPLICATION,msg);
	SYSLOG_APPLICATION_INFO_LOGGING("Exit function app_send_message_to_transport_input_queue_func");
}
/***********************************************************************
 * FUNCTION NAME:
 *  main
 *
 * DESCRIPTION:
 * initial all global variable and other initialization.
 * initial for output_message_queue of application_layer
 *
 * INPUT:
 *
 * OUTPUT:
 *
 * RETURN:
 *	val_result
 * GLOBAL DATA REFERRED:
 *
 * CALLED ROUTINES:
 *
 * NOTES:
 *
 ************************************************************************/
U8 main(U8 *argv,U32 argc)
{
	app_init_func();
	app_init_list_message_func();
	app_send_message_to_transport_input_queue_func();
	return SYSLOG_SUCCESS;
}
